package cn.jstars.datatocloud.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Spring Util Class
 * 
 * @version 2018/05/07 14:37:32
 * @author zhu_longxiang
 *
 */
@Component
public class SpringUtil implements ApplicationContextAware {
	/** ApplicationContext Object **/
	private static ApplicationContext applicationContext;

	/**
	 * init applicationContext
	 */
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		if (SpringUtil.applicationContext == null) {
			SpringUtil.applicationContext = applicationContext;
		}
	}

	/**
	 * get ApplicationContext
	 * 
	 * @return ApplicationContext
	 */
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	/**
	 * get bean by name
	 * 
	 * @param name
	 *            name
	 * @return Object
	 */
	public static Object getBean(String name) {
		return getApplicationContext().getBean(name);
	}

	/**
	 * get bean by class
	 * 
	 * @param clazz
	 *            class
	 * @return Object
	 */
	public static <T> T getBean(Class<T> clazz) {
		return getApplicationContext().getBean(clazz);
	}

	/**
	 * get bean by class and name
	 * 
	 * @param name name
	 * @param clazz
	 *            class
	 * @return Object
	 */
	public static <T> T getBean(String name, Class<T> clazz) {
		return getApplicationContext().getBean(name, clazz);
	}
}
